package com.symc.his.api.impl.doctor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.symc.his.api.base.BaseApiService;
import com.symc.his.api.base.BaseResponse;
import com.symc.his.api.doctor.DiagnosisService;
import com.symc.his.api.doctor.dto.req.DiagnosisReqDTO;
import com.symc.his.api.doctor.dto.resp.DiagnosisRespDTO;
import com.symc.his.api.impl.entity.DiagnosisDO;
import com.symc.his.api.impl.mapper.DiagnosisMapper;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @CreateTime: 2023/4/21 21:02
 * @Version:
 * @Since: 1.8
 * @Author: 辛凤文 forward.seen@foxmail.com
 */
@RestController
public class DiagnosisServiceImpl extends BaseApiService implements DiagnosisService {
    @Resource
    private DiagnosisMapper diagnosisMapper;

    @Override public BaseResponse<DiagnosisRespDTO> getDiagnosisByVisitRecordId(String visitRecordId) {
        LambdaQueryWrapper<DiagnosisDO> queryWrapper = new LambdaQueryWrapper<DiagnosisDO>().eq(DiagnosisDO::getVisitRecordId, visitRecordId);
        DiagnosisDO diagnosisDO = diagnosisMapper.selectOne(queryWrapper);
        DiagnosisRespDTO diagnosisRespDTO = DO2DTO(diagnosisDO, DiagnosisRespDTO.class);
        return setResultSuccess(diagnosisRespDTO);
    }

    @Transactional(rollbackFor = SQLException.class)
    @Override public BaseResponse<DiagnosisRespDTO> saveDiagnosis(DiagnosisReqDTO diagnosisReqDTO) {
        String visitRecordId = diagnosisReqDTO.getVisitRecordId();
        if (StringUtils.isEmpty(visitRecordId)) {
            return setResultError("visitRecordId不能为null");
        }
        LambdaQueryWrapper<DiagnosisDO> queryWrapper = new LambdaQueryWrapper<DiagnosisDO>().eq(DiagnosisDO::getVisitRecordId, visitRecordId);
        DiagnosisDO selectDiagnosisDO = diagnosisMapper.selectOne(queryWrapper);
        DiagnosisDO diagnosisDO = DTO2DO(diagnosisReqDTO, DiagnosisDO.class);
        int rowRes = 0;
        if (selectDiagnosisDO == null) {
            rowRes = diagnosisMapper.insert(diagnosisDO);
        } else {
            rowRes = diagnosisMapper.update(diagnosisDO, queryWrapper);
        }
        if (rowRes >= 1) {
            return setResultSuccess("保存成功!");
        }
        return setResultError("保存失败!");
    }
}